//
//  NormalQuestionViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/15.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "NormalQuestionViewController.h"
#import "NormalProblemTableViewCell.h"
@interface NormalQuestionViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)NSMutableArray * cellData;
@property (nonatomic, strong)NSMutableArray * btnStateArr;
@end

@implementation NormalQuestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"常见问题";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self makeData];//制作数据
    [self.view addSubview:self.tableView];
    
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.frame = CGRectMake(0, 0/WIDTH_6_SCALE, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[NormalProblemTableViewCell class] forCellReuseIdentifier:NSStringFromClass([NormalProblemTableViewCell class])];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.headerView;
        _tableView.rowHeight = 42/WIDTH_6_SCALE;
    }return _tableView;
}

//btnState:0代表收起，1代表打开
- (NSMutableArray *)cellData{
    if (!_cellData) {
        _cellData = @[
  @{@"title":@"若遇不幸，怎么申请互助1", @"content":@"内容1"},
  @{@"title":@"若遇不幸，怎么申请互助2", @"content":@"内容2"},
  @{@"title":@"若遇不幸，怎么申请互助3", @"content":@"内容3"},
  @{@"title":@"若遇不幸，怎么申请互助4", @"content":@"内容4"},
  @{@"title":@"若遇不幸，怎么申请互助5", @"content":@"内容5"},
  @{@"title":@"若遇不幸，怎么申请互助6", @"content":@"内容6"},
  @{@"title":@"若遇不幸，怎么申请互助7", @"content":@"内容7"},
  @{@"title":@"若遇不幸，怎么申请互助8", @"content":@"内容8"},
  @{@"title":@"若遇不幸，怎么申请互助9", @"content":@"内容9"},
  @{@"title":@"若遇不幸，怎么申请互助10", @"content":@"内容10"}].mutableCopy;
    }return _cellData;
}
- (void)makeData{
    self.btnStateArr = [NSMutableArray array];
    for (int i=0; i<self.cellData.count; i++) {
        [self.btnStateArr addObject:@"0"];
    }
}
#pragma mark - talbleViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NormalProblemTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NormalProblemTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.cellData[indexPath.section];
    cell.clipsToBounds = YES;//这句话很重要 不信你就试试
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.cellData.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.btnStateArr[indexPath.section] isEqualToString:@"0"]) {
        return 1;
    }else{
        return [NormalProblemTableViewCell heightForCellWithComment:self.cellData[indexPath.section]];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * whiteBGView = [[UIView alloc] init];//白色背景
    whiteBGView.backgroundColor = [UIColor whiteColor];
    UILabel * titleLab = [[UILabel alloc] init];//头部真正的标题
    UIImageView * arrowView = [[UIImageView alloc] init];//箭头按钮
    if (section == 0 || section == 4 || section == 6 || section == 8) {
        whiteBGView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 81/WIDTH_6_SCALE);
        UIView * upSectionHeaderBGView = [[UIView alloc] init];
        upSectionHeaderBGView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40/WIDTH_6_SCALE);
        upSectionHeaderBGView.backgroundColor = [UIColor whiteColor];
        [whiteBGView addSubview:upSectionHeaderBGView];
        UILabel * headerLab = [[UILabel alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 0, SCREEN_WIDTH, upSectionHeaderBGView.height)];
        [self label:headerLab withFont:DEF_FontSize_13 withText:@"" withTextColor:getColor(@"9b9b9b") withTextAlignment:NSTextAlignmentLeft withView:upSectionHeaderBGView withBGColor:[UIColor clearColor]];
        UIView * borderLine = [[UIView alloc] initWithFrame:CGRectMake(0, upSectionHeaderBGView.height - 1/WIDTH_6_SCALE, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
        borderLine.backgroundColor = getColor(@"efefef");
        [upSectionHeaderBGView addSubview:borderLine];
        titleLab.frame = CGRectMake(18/WIDTH_6_SCALE, upSectionHeaderBGView.height, SCREEN_WIDTH - 36/WIDTH_6_SCALE, 40/WIDTH_6_SCALE);
        if (section == 0) {
            headerLab.text = @"互助介绍";
        }else if (section == 4){
            headerLab.text = @"互助规则";
        }else if (section == 6){
            headerLab.text = @"安全保障";
        }else if (section == 8){
            headerLab.text = @"加入规则";
        }
    }else{
        titleLab.frame = CGRectMake(18/WIDTH_6_SCALE, 0, SCREEN_WIDTH - 36/WIDTH_6_SCALE, 40/WIDTH_6_SCALE);
    }
    [self label:titleLab withFont:DEF_FontSize_13 withText:[self.cellData[section] objectForKey:@"title"] withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentLeft withView:whiteBGView withBGColor:[UIColor clearColor]];
    arrowView.frame = CGRectMake(SCREEN_WIDTH - 27/WIDTH_6_SCALE, 0, 11/WIDTH_6_SCALE, 7/WIDTH_6_SCALE);
    arrowView.centerY = titleLab.centerY;
    arrowView.image = [UIImage imageNamed:@"down_icon"];
    [whiteBGView addSubview:arrowView];
    UIButton * arrowBtn = [[UIButton alloc] initWithFrame:CGRectMake(arrowView.left, titleLab.top, arrowView.width + 16/WIDTH_6_SCALE, titleLab.height)];
    [arrowBtn addTarget:self action:@selector(arrowBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [arrowBtn setBackgroundColor:[UIColor clearColor]];
    [whiteBGView addSubview:arrowBtn];
    whiteBGView.tag = 7000+section;
    return whiteBGView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 4 || section == 6 || section == 8) {
        return 81/WIDTH_6_SCALE;
    }
    return 40/WIDTH_6_SCALE;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 88;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
#pragma mark - action
- (void)arrowBtnClick:(UIButton *)sender{
    UIView *bgView = [sender superview];//获取父类view
    NSInteger tempIndex = bgView.tag;
    int clickIndex = (int)tempIndex - 7000;
    NSLog(@"%i", clickIndex);
    //展开
    if ([self.btnStateArr[clickIndex] isEqualToString:@"0"]) {
        self.btnStateArr[clickIndex] = @"1";
//        [_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationBottom];  //使用下面注释的方法就 注释掉这一句
    } else { //收起
        self.btnStateArr[clickIndex] = @"0";
//        [_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop]; //使用下面注释的方法就 注释掉这一句
    }
    NSRange range = NSMakeRange(clickIndex, 1);
    NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
    [_tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationAutomatic];

}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 9/WIDTH_6_SCALE)];
    footerView.backgroundColor = getColor(@"f5f5f5");
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 3||section == 5) {
        return 9/WIDTH_6_SCALE;
    }return 0;
}
@end
